package com.liwenfeng.huangputi;

import java.util.*;
import java.util.stream.Collectors;

/*
 * 班级组织团建，有四个去处，80个同学投票，可以多选，可以不选，模拟投票，统计投票结果
 * */
public class GameDemo {
    public static void main(String[] args) {
//        new Room().start();
        //班级里有80个同学，秋游的地点有4个备选，统计投票结果：
        Map<String, List<String>> map = new HashMap<>();
        String[] selects = {"A", "B", "C", "D"};
        for (int i = 0; i < 80; i++) {
            ArrayList<String> list = new ArrayList<>();
            for (int y = 0; y < 4; y++) {
                int selectIndex = (int) (Math.random() * 8);
                if (selectIndex < 4 && !list.contains(selects[selectIndex])) {
                    list.add(selects[selectIndex]);
                }
            }
            map.put("zhang" + (i + 1), list);
        }
        System.out.println(map);

        //普通遍历方式统计
        List<String> noSelect1 = new ArrayList<>();
        Map<String, Long> collect1 = new HashMap<>();
        for (String s : map.keySet()) {
            List<String> list = map.get(s);
            if (list.size()==0){
                noSelect1.add(s);
            }
        }
        for (List<String> values : map.values()) {
            for (String value : values) {
                if (value!=null) collect1.put(value, collect1.get(value) == null ? 1 : collect1.get(value) + 1);
            }
        }
        System.out.println("没有选择的同学："+noSelect1);
        System.out.println("统计结果为："+collect1);
        System.out.println("-------------------------------------------");

        //流方式统计，先分组再统计个数
        List<String> noSelects2 = map.entrySet().stream()
                .filter(item -> item.getValue().size() == 0)
                .map(Map.Entry::getKey).toList();
        System.out.println("没有选择的同学："+noSelects2);

        Map<String, Long> collect2 = map.values().stream().flatMap(Collection::stream)
                .collect(Collectors.groupingBy(t -> t, Collectors.counting()));
        System.out.print("统计结果：");
        List<Map.Entry<String, Long>> desList = collect2.entrySet().stream().sorted(((o1, o2) -> (int) (o2.getValue() - o1.getValue()))).toList();
        System.out.println(desList);
        Long max = desList.get(0).getValue();

        List<String> maxObject = desList.stream().filter(item -> Objects.equals(item.getValue(), max)).map(Map.Entry::getKey).toList();
        System.out.println("投票最多的票数为："+max);
        System.out.println("投票最多的项目为："+maxObject);

        for (String s : maxObject) {
            List<String> entries = map.entrySet().stream()
                    .filter(item -> !item.getValue().contains(s))
                    .map(Map.Entry::getKey)
                    .toList();
            System.out.println("没有投票"+s+"项目的有："+entries);
        }
    }
}
